<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/manhome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>文书列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!--用户列表主部分-->
        <el-card>
            <el-row :gutter="25">
                <el-col span="10">
                <!--搜索区--> 
                   <el-input placeholder="输入搜索内容" v-model="queryInfo.query" clearable @clear="getEssayList">
                    <el-button slot="append" icon="el-icon-search" @click="getEssayList"></el-button>
                   </el-input>
                </el-col>
            <!--添加区-->
                <el-col span="10">
                 <el-button type="primary" @click="addDialogVisible=true">添加</el-button>
                 
                </el-col>
            </el-row>
            <!--表格 border边框 stripe隔行变色-->
            <el-table :data="stuentList" border stripe>
                <el-table-column label="文书Id" prop="Id"></el-table-column>
                <el-table-column label="标题" prop="Title"></el-table-column>
                <el-table-column label="年份" prop="Year"></el-table-column>
                <el-table-column label="内容" prop="Content"></el-table-column>
                <el-table-column label="操作" prop="Action"></el-table-column>
           <template >
               <!--<template slot-scope="scope">-->
               <el-button type="primary" icon="el-icon-edit" size="mini" >修改</el-button>
               <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
               <!--<el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(Id)">修改</el-button>-->
               <!--<el-button type="danger" icon="el-icon-delete" size="mini" @click="delete(Id)">删除</el-button>-->
           </template>
            </el-table>
            <!--分页组件-->
            <div>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[5, 10, 20, 30]"
                    :page-size="queryInfo.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </el-card>
        <!--添加区域弹窗-->
        <el-dialog title="添加文书" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="文书Id" prop="Id">
                    <el-input v-model="editForm.Id"></el-input>
                </el-form-item>
                <el-form-item label="标题" prop="Title">
                    <el-input v-model="editForm.Title"></el-input>
                </el-form-item>
                <el-form-item label="年份" prop="Year">
                    <el-input v-model="editForm.Year"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="Content">
                    <el-input v-model="editForm.Content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="addDialogVisible">取消</el-button>
                <el-button type="primary" @click="addStudent">添加</el-button>
            </span>
        </el-dialog>
        <!--修改区域弹窗-->
        <el-dialog title="修改文书信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="文书Id" prop="Id">
                    <el-input v-model="editForm.Id" disabled></el-input>
                </el-form-item>
                <el-form-item label="标题" prop="Title">
                    <el-input v-model="editForm.Title"></el-input>
                </el-form-item>
                <el-form-item label="年份" prop="Year">
                    <el-input v-model="editForm.Year"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="Content">
                    <el-input v-model="editForm.Content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="editDialogVisible">取消</el-button>
                <el-button type="primary" @click="editEssay">确认修改</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default{
        created(){
           this.getEssayList();
        },
        data(){
            return{
                //查询信息实体
                queryInfo:{
                    query:"",
                    pageNum:1,
                    pageSize:5,
                },
                essayList:[],//文书列表
                total:0,
                //对话框状态
                addDialogVisible:false,
                addDialogVisible:false,
                //表单信息
                addForm:{
                    Id:"",
                    Title:"",
                    Year:"",
                    Content:""
                },
                editForm:{
                    Id:"",
                    Title:"",
                    Year:"",
                    Content:""
                },
                //表单验证
                addFormRules:{
                    Id: [
                        { required: true, message: '请输入文书Id', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5到 10 个字符', trigger: 'blur' }
                          ],
                    Title: [
                        { required: true, message: '请输入文书标题', trigger: 'blur' },
                        { min: 4, max: 8, message: '长度在 4到 8个字符', trigger: 'blur' }
                          ],
                    Year: [
                        { required: true, message: '请输入文书年份', trigger: 'blur' },
                        { min: 1, max: 20, message: '长度在 4到 8个字符', trigger: 'blur' }
                          ],      
                    Content: [
                        { required: true, message: '请输入文书内容', trigger: 'blur' },
                        { min: 0, max: 500, message: '长度在 3到 5个字符', trigger: 'blur' }
                          ],      
                     },
                editFormRules:{
                    Title: [
                        { required: true, message: '请输入文书标题', trigger: 'blur' },
                        { min: 4, max: 8, message: '长度在 4到 8个字符', trigger: 'blur' }
                          ],
                    Year: [
                        { required: true, message: '请输入文书年份', trigger: 'blur' },
                        { min: 1, max: 20, message: '长度在 4到 8个字符', trigger: 'blur' }
                          ], 
                    Content: [
                        { required: true, message: '请输入文书内容', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3到 5个字符', trigger: 'blur' }
                          ],      
                     },
            }

        },
        methods:{
           async getEssayList(){
                const {data:res} =await this.$http.get("allessay",{params:this.queryInfo});
                this.essayList=res.data;//列表数据封装
                this.total=res.number;//总数封装
            },
            //最大数
            handleSizeChange(newSize){
                this.queryInfo.pageSize=newSize;
                this.getEssayList();
            },
            //pageNum的触发
            handleCurrentChange(newPage){
                this.queryInfo.pageNum=newPage;
                this.getEssayList();
            },
            //添加
            //监听添加操作
            addDialogClose(){
                this.$refs.addFormRef.resetFields();
            },
            addEssay(){
                this.$refs.addFormRef.validate(async valid=>{
                   if(!valid) return;
                   /*const {data:res}=await this.$http.post("addEssay",this.addForm);
                   if(res!="success"){
                       return this.$message.error("添加失败！");
                   }else{
                   this.$message.error("添加成功！");
                   this.addDialogVisible=false;
                   this.getEssayList();
                   }
                });
                   */
                    const {data:res}="ok";
                    if(res=="ok"){
                    this.$message.error();("操作失败！");
                    
                    }else{
                    this.$message.success("操作成功！");
                    this.addDialogVisible=false;
                    }
                });      
            },
            //删除
            async delete(Id){
               const confirmResult=await this.$confirm('此操作将永久删除，是否继续','提示',{
                    confirmButtonText:'确定',
                    cancleButtonText:'取消',
                    type:'warning'
                }).catch(err=>err)
                if(confirmResult!='confirm'){
                    return this.$message.Info('已取消删除');
                }
                const {data:res}=await this.$http.delete("deleteEssay?Id="+Id);
                if(res!="success"){
                    return this.$message.error("删除失败！");
                }
                this.$message.success("删除成功！");
                this.getEssayList();
            },
            //修改
            //显示对话框，按钮触发
            async showEditDialog(Id){
                const {data:res}=await this.$http.get("editEssay?Id="+Id);
                this.editForm=res;//存储查询信息
                this.editDialogVisible=true;//开启编辑框
            },
            //监听修改操作
            editDialogClose(){
                this.$refs.editFormRef.resetFields();//关闭窗口，重置信息
            },
            //确认修改
            editEssay(){
                this.$refs.editFormRef.validate(async valid=>{
                    if(!valid) return;
                   /*const {data:res}=await this.$http.put("editEssay",this.editForm);
                   if(res!="success"){
                       return this.$message.error("修改失败！");
                   }else{
                   this.$message.error("修改成功！");
                   this.editDialogVisible=false;
                   this.getEssayList();
                   }
                });
                   */
                   const {data:res}="ok";
                    if(res=="ok"){
                    this.$message.error();("修改失败！");
                    
                    }else{
                    this.$message.success("修改成功！");
                    this.editDialogVisible=false;
                    }
                });
            },
        }

    }
</script>
<style  scoped>
.el-breadcrumb{
    margin-bottom: 15px;
    font-size: 15px;
}
</style>